package descentFly.controller
{
	import UIFree.ObjectEvent;
	
	import descentFly.app.radar.df_radarEvent;
	import descentFly.chips.df_chip;
	import descentFly.df_space;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class df_robotController extends FlyController {
		public static const STATE_IDLE		: int = 0;
		public static const STATE_PATROL	: int = 1;
		public static const STATE_TRACKING	: int = 2;
		public static const STATE_DRIVE		: int = 3;
		public static const STATE_ATACK		: int = 4;
		
		public static const FIELD_SEE		: String = 'see';
		public static const FIELD_ATACK		: String = 'atack';
		
		public static const CHANGE_SEE		: String	= 'CHANGE_SEE';
		public static const CHECKSEEPERIOD	: int 		= 10;
		
		
		private var _see			: Boolean;
		private var _checkSeeIndex	: int;
		private var _state			: int;
		
		public static function create(a_stage	: Stage,
									  a_space	: df_space,
									  a_options	: Object = null): df_robotController {
			return new df_robotController(a_stage, a_space);
		}
		
		public function df_robotController(a_stage	:Stage, 
										   a_space	: df_space,
										   dispatchTarget :EventDispatcher=null, a_options : Object=null) {
			super(a_stage, a_space, dispatchTarget, a_options);
			_state = STATE_IDLE;
			_checkSeeIndex = 0;
		}
		
		protected function get state(): int {
			return _state;
		}
		
		protected function set state(a_state: int): void {
			if (_state != a_state) setState(a_state);
		}
		
		protected function setState(a_state: int): void {
			_state = a_state;
			if (_state == STATE_ATACK) playSound(FIELD_ATACK, _body);
		}
		
		protected function idle(): void {
		} 
		
		protected function patrol(): void {
		} 
		
		protected function tracking(): void {
		} 
		
		protected function drive(): void {
		} 
		
		protected function atack(): void {
		}
		
		protected function checkSee(isDir: Boolean=true): Boolean {
			return false;
		}
		
		protected function doChangeSee(): void {
			dispatchEvent(new Event(CHANGE_SEE));
			if (_see) playSound(FIELD_SEE, _body);
		}
		
		protected function get see(): Boolean {
			return _see;
		}
			
		protected function set see(a_see: Boolean): void {
			if (_see != a_see) {
				_see = a_see;
				doChangeSee();
			}
		}
		
		override public function typeFromRadar(): int {
			return df_radarEvent.TYPE_ROBOT;
		}
		
		override protected function doFrame(): void {
			if (_checkSeeIndex == 0) {
				see = checkSee();
			}
			_checkSeeIndex = (_checkSeeIndex + 1) % CHECKSEEPERIOD;
			
			switch (_state) {
				case STATE_IDLE		: idle(); 
					break;
				case STATE_PATROL	: patrol(); 
					break;
				case STATE_TRACKING	: tracking();
					break;
				case STATE_DRIVE	: drive();
					break;
				case STATE_ATACK	: atack();
					break;
			}
			super.doFrame();
		}
	}
}